yjx.factory('$init', ['$rootScope', 'ipCookie', '$timeout', '$location', '$state','detailService','urlCgService',
function ($rootScope, ipCookie, $timeout, $location, $state,detailService,urlCgService) {
    return {
        cleanCookies: function () {
            $timeout(function () {
                //清除$roosScope cookie 缓存
                $rootScope.userId = null;
                $rootScope.user = null;
                $rootScope.identity = null;
                $rootScope.token = null;
                ipCookie.remove('userId', ipCookiesParam);
                ipCookie.remove('user', ipCookiesParam);
                ipCookie.remove('identity', ipCookiesParam);
                ipCookie.remove('token', ipCookiesParam);
            })
        },

        /*用户登录后将信息缓存至ipCookie 、$rootScope的赋值*/
        initLoginData: function(data,pCookieParams) {
            if(data) {
                // alert(JSON.stringify(data.user))
                var cookieParams = pCookieParams || ipCookiesParam;
                ipCookie('userId', data.user.id, cookieParams);
                ipCookie('user', data.user, cookieParams);
                ipCookie('identity',$rootScope.identityTo,cookieParams);
                ipCookie('token', data.token, cookieParams);
                ipCookie('showBrokerage',data.user.showBrokerage,cookieParams)

                $rootScope.userId = data.user.id;
                $rootScope.user = data.user;
                $rootScope.token = data.token;
                $rootScope.identity = $rootScope.identityTo;
                $rootScope.showBrokerage = data.user.showBrokerage
            }
        },
        initLoginUrl: function (absurl) {
            if($rootScope.identityTo == 1 ){
                if(!$rootScope.inviteId){
                    /*小B入口进入*/
                    if($rootScope.user.telephone){
                        $rootScope.identity =1 ;
                        $state.go("index")
                    }else{
                        detailService.openDetail('register');
                    }
                }else{
                    /*邀请加入团队进入*/
                    detailService.openDetail("joinTeam");
                }
            }else{
                /*其他路径进行默认消费者身份*/
                if($rootScope.user.telephone){
                    $rootScope.identity = 0;
                    $timeout(function () {
                        if ((absurl && absurl.split('/#/') && absurl.split('/#/').length > 1) || (absurl && absurl.split('/') && absurl.split('/').length > 1)) {
                            urlCgService.encodeUrl(absurl);
                        } else {
                            $state.go("index");
                        }
                    },1000)
                }else{
                    detailService.openDetail("register")
                }
            } 
        },

        title:function(title){
            title = title?title:'基因云';
            angular.element(document.querySelector('#title')).html(title)
        },
       
    }
}]);